$(function () {
    $('.login_').click(function () {
        $('.marking').fadeIn()
    })
    $('.closeBtn').click(function () {
        $('.marking').fadeOut()
    })
    // 登录
    $("#loginSubmit1").click(function () {
        var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        $.ajax({
            url: "/activity/library/login",
            type: "POST",
            data: ({
                m: $("#phone").val().replace(/\s+/g, ""),
                c: $("#phonecode").val().replace(/\s+/g, ""),
            }),
            beforeSend: function () {

                sessionStorage.setItem("Storage_url", window.location);
                var username = $("#phone").val().replace(/\s+/g, "");
                if (!(myreg.test(username))) {
                    $("#errorInfo1").html("请输入正确的手机号!");
                    $("#checkPhone1").show();
                    return false;
                }
                // var captcha = $("#captcha").val();
                // if (captcha == "") {
                //     $("#errorInfo1").html("请输入图形验证码!");
                //     $("#checkPhone1").show();
                //     return false;
                // }
                var code = $("#phonecode").val().replace(/\s+/g, "");
                if (code == "") {
                    $("#errorInfo1").html("请输入验证码!");
                    $("#checkPhone1").show();
                    return false;
                }

            },
            success: function (data) {
                console.log(data)
                if (data.code == 200) {
                    $("#loginSubmit1").html("登录中...");
                    setTimeout(function () {
                        $('.marking').css({
                            'display': 'none'
                        })
                        let url = sessionStorage.getItem("Storage_url");
                        if (url) {
                            location.href = url;
                        } else {
                            location.href = 'https://mk.yanxian.org/library';
                        }
                    }, 1000);
                } else {
                    $("#errorInfo1").html(data.msg);
                    $("#checkPhone1").show();
                }
            }
        });
    })

    // 获取验证码 ////////
    $("#phonecodeDiv").click(function () {
        var username = $("#phone").val().replace(/\s+/g, "");
        phone_code(this, username);
    });

    function phone_code(obj, targetData) {
        var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        var timestamp = new Date().getTime();
        var phone = $("#phone").val();
        var sign1 = phone + timestamp + 'success';
        var sign = hex_md5(sign1);
        //验证移动电话
        var data = targetData;
        if (!(myreg.test(data))) {
            $("#errorInfo1").html("请输入正确的手机号！");
            $("#checkPhone1").show();

            return false;
        }
        //验证登录名称是否重复
        $.ajax({
            url: "/activity/labor/send_sms",
            type: "post",
            data: {
                "m": data,
                "s": sign,
                "t": timestamp,
            },
            success: function (data) {
                if (data.code == 200) {
                    invokeSettime(obj);
                    $('#errorInfo1').html(data.msg)
                    $("#checkPhone1").show();
                } else {
                    $("#errorInfo1").html(data.msg);
                    $("#checkPhone1").show();
                }
            }
        });
    }

    function invokeSettime(obj) {
        var countdown = 60;
        settime(obj);

        function settime(obj) {
            if (countdown == 0) {
                $(obj).attr("disabled", false);
                $(obj).html("获取验证码");
                countdown = 60;
                return;
            } else {
                $(obj).attr("disabled", true);
                $(obj).html("(" + countdown + ") s 重新发送");
                countdown--;
            }
            setTimeout(function () {
                settime(obj)
            }, 1000)
        }
    }

    $(document).on('click', '.closeBtn', function () {
        $('.marking').hide();
    })
})